<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="upAuthForm('0',1)">添加权限</el-button>
        </div>
        <el-table v-loading="this.$store.state.logoing" border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" size="small" type="text" @click="upAuthForm(row.id, 2)">添加权限点</el-button>
              <el-button size="small" type="text" @click="toEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="authFormX">
      <!-- 表单内容 -->
      <el-form ref="authForm" v-loading="$store.state.logoing" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="是否展示" prop="enVisible">
          <el-switch v-model="formData.enVisible" active-value="0" inactive-value="1" active-text="启用" inactive-text="禁用" active-color="#13ce66" inactive-color="#eee" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { translateListToTreeData } from '@/utils/index.js'
export default {
  name: 'Permission',
  data () {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '权限点名称不能为空', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '权限点编码不能为空', trigger: ['blur', 'change'] }
        ]
      },
      typeAuthForm: 0
    }
  },
  created () {
    this.loadPermissionList()
  },
  methods: {
    // 加载权限列表数据
    async loadPermissionList () {
      try {
        const ret = await reqGetPermissionList()
        if (ret.code === 10000) {
          this.list = translateListToTreeData(ret.data, '0')
        } else {
          this.$message.error('获取权限列表失败')
        }
      } catch {
        this.$message.error('获取权限列表失败')
      }
    },
    // 编辑弹窗
    upAuthForm (id, type) {
      this.typeAuthForm = 0
      this.formData.type = type
      this.formData.pid = id
      this.showDialog = true
    },
    // 关闭编辑弹窗后
    authFormX () {
      this.showDialog = false
      this.$refs.authForm.resetFields()
    },
    // 提交编辑表单
    handleSubmit () {
      if (this.typeAuthForm === 0) {
        this.$refs.authForm.validate(async valid => {
          if (!valid) return
          try {
            const ret = await reqAddPermission(this.formData)
            if (ret.code === 10000) {
              // 关闭弹窗
              this.showDialog = false
              // 刷新列表
              this.loadPermissionList()
              this.$message.error(ret.message)
            } else {
              this.$message.error(ret.message)
            }
          } catch {
            this.$message.error('提交失败')
          }
        })
      } else if (this.typeAuthForm === 1) {
        this.$refs.authForm.validate(async valid => {
          if (!valid) return
          try {
            const ret = await reqUpdatePermission(this.formData)
            if (ret.code === 10000) {
              // 关闭弹窗
              this.showDialog = false
              // 刷新列表
              this.loadPermissionList()
              this.$message.error(ret.message)
            } else {
              this.$message.error(ret.message)
            }
          } catch {
            this.$message.error('更新失败')
          }
        })
      }
    },
    // 删除
    async handleDelete (id) {
      try {
        const ret = await reqDelPermission(id)
        if (ret.code === 10000) {
          this.loadPermissionList()
          this.$message.success(ret.message)
        }
      } catch {
        this.$message.error('删除失败')
      }
    },
    // 查看权限编辑弹窗
    async toEdit (id) {
      this.showDialog = true
      this.typeAuthForm = 1
      try {
        const ret = await reqGetPermissionDetail(id)
        if (ret.code === 10000) {
          this.formData = ret.data
        }
      } catch {
        this.$message.error('查看失败')
      }
    }
  }
}
</script>
